<template>
  <div class="kxy-container">
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @refreshDataList="getDataList" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border style="flex: 1" height="100%" v-loading="dataListLoading">
      <el-table-column align="center" type="index" header-align="center" width="80px" label="序号"/>
      <el-table-column align="center" prop="name" header-align="center" label="广告名称"/>
      <el-table-column align="center" prop="adPositionName" header-align="center" label="广告位置"/>
      <el-table-column align="center" prop="coverUrl" header-align="center" label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.coverUrl" width="200px" height="100px"/>
        </template>
      </el-table-column>
      <!--<el-table-column label="排序" align="center" width="80">
                <template slot-scope="scope">
                    <span>{{scope.row.sort}}</span>
                </template>
      </el-table-column>-->
      <!--<el-table-column
                    align="center"
                    prop="val"
                    header-align="center"
      label="广告链接"/>-->
      <el-table-column header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="text" @click="delOrUpdateHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    />
    <!--    弹窗，新增/修改-->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './advertisement-add'

  export default {
    name: 'notice',
    data() {
      return {
        dataForm: {
          keyWord: ''
        },
        dataList: [],
        dataListSelections: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        addOrUpdateVisible: false,
        editOrUpdateVisible: false,
        dataListLoading: false
      }
    },
    components: {
      AddOrUpdate
    },
    mounted() {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/news-admin/adver/list'),
          method: 'post',
          data: {
            page: this.pageIndex,
            limit: this.pageSize
          }
        }).then(({data}) => {
          if (data) {
            this.dataList = data.list
            this.totalPage = data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      delOrUpdateHandle(id) {
        this.$confirm(`确定要进行删除操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$http({
              url: this.$http.adornUrl('/news-admin/adver/delete'),
              method: 'post',
              data: {
                ids: id || this.dataListSelections.map(item => item.id).join(',')
              }
            }).then(data => {
              if (data && data.code === 200) {
                this.$notify.success({
                  title: '提示',
                  message: '操作成功'
                })
                this.pageIndex > 1 && this.dataList.length <= 1 && this.pageIndex --
                this.getDataList()
              } else {
                this.$notify.warning({
                  title: '提示',
                  message: data.msg
                })
              }
            })
          })
          .then(({data}) => {
            this.dataListLoading = false
          })
      },
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      editOrUpdateHandle(id) {
        this.editOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.editOrUpdate.init(id)
        })
      }
    }
  }
</script>

<style scoped>
</style>
